

/**
 * Copyright (2024, ) Institute of Software, Chinese Academy of Sciences
 * author: wuheng@iscas.ac.cn
 * since:  0.1.0
 *
**/

use std::env;
use diesel::pg::PgConnection;
use diesel::{update, Connection, ConnectionError};
use diesel::dsl::delete;
use status_manager::cores::create::create_posgres_table;
use crate::cores::create::{create_sqlite3_table, insert_postgres_object, insert_sqlite3_object};
use crate::cores::delete::{delete_postgres_object, delete_sqlite3_object, drop_postgres_table, drop_sqlite3_table};
use crate::cores::query::{list_postgres_object, list_sqlite3_object, query_postgres_table, query_sqlite3_table};
use serde_json::json;
use crate::cores::update::{update_postgres_object, update_sqlite3_object};
use std::collections::HashMap;
use diesel::SqliteConnection;

mod cores;

static POSTGRES_URL: &str =  "postgresql://postgres:onceas@192.168.42.134:30306/fleet";

static SQLITE3_URL: &str =  "/data/database.db";

fn main()
{

    let json_data_to_insert = json!({
        "name": "test",
        "namespace": "test",
        "apigroup": "iscas/v1",
        "data": {
            "key": "value"
        }
    });

    let json_data_to_update = json!({
        "name": "test",
        "namespace": "test",
        "apigroup": "iscas/v1",
        "data": {
            "key": "newvalue"
        }
    });

    // match establish_postgres_connection() {
    //     Ok(mut conn) => {
    //         println!("connecting successfully.");
    //         create_posgres_table(&mut conn, "pods");
    //         let exist: bool = query_postgres_table(&mut conn, "pods").unwrap();
    //         println!("table exist: {}", exist);
    //         let delete: bool = drop_postgres_table(&mut conn, "pods").expect("REASON");
    //         println!("table exist: {}", delete);
    //         insert_postgres_object(&mut conn, "pods", json_data_to_insert.clone());
    //         update_postgres_object(&mut conn, "pods", json_data_to_update.clone());
    //         delete_postgres_object(&mut conn, "pods", json_data_to_insert.clone());
    //         println!{"{:?}", list_postgres_object(&mut conn, "pods", HashMap::new(), 1, 10)}
    //     },
    //     Err(e) => {
    //         eprintln!("Error connecting to the database: {}", e);
    //     },
    // }

    match establish_sqlite3_connection() {
        Ok(mut conn) => {
            println!("connecting successfully.");
            // match create_sqlite3_table(&mut conn, "pods") {
            //     Ok(_) => println!("Table `{}` created successfully.", "pods"),
            //     Err(e) => eprintln!("Error creating table: {:?}", e),
            // }
            // let exist: bool = query_sqlite3_table(&mut conn, "pods").expect("REASON");
            // println!("{}", exist);

            // drop_sqlite3_table(&mut conn, "podds");

            // match insert_sqlite3_object(&mut conn, "pods", json_data_to_insert.clone()) {
            //     Ok(_) => println!("Object `{:?}` created successfully.", json_data_to_insert.clone()),
            //     Err(e) => eprintln!("Error insert object: {:?}", e),
            // }

            // match update_sqlite3_object(&mut conn, "pods", json_data_to_update.clone()) {
            //     Ok(_) => println!("Object `{:?}` created successfully.", json_data_to_update.clone()),
            //     Err(e) => eprintln!("Error insert object: {:?}", e),
            // }

            // println!{"{:?}", list_sqlite3_object(&mut conn, "pods", HashMap::new(), 1, 10)}

            // delete_sqlite3_object(&mut conn, "pods", json_data_to_insert.clone());
        },
        Err(e) => {
            eprintln!("Error connecting to the database: {}", e);
        },
    }
}

fn establish_postgres_connection() -> Result<PgConnection, ConnectionError> {
    PgConnection::establish(POSTGRES_URL)
}

fn establish_sqlite3_connection() -> Result<SqliteConnection, ConnectionError> {
    SqliteConnection::establish(SQLITE3_URL)
}